package com.bzframework.core.redisson;

import lombok.RequiredArgsConstructor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;

/**
 * RedissonAutoConfiguration
 */
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "bzframework.redisson", name = "enabled", havingValue = "true")
public class RedissonAutoConfiguration {

    private final RedisProperties redisProperties;

    @Bean
    @ConditionalOnMissingBean(RedissonClient.class)
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer()
              .setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort())
              .setPassword(redisProperties.getPassword())
              .setDatabase(redisProperties.getDatabase());
        return Redisson.create(config);
    }


}
